import {createStore,combineReducers,compose,applyMiddleware} from 'redux';
import ThunkMiddleware from 'redux-thunk';
import immutablestate from 'redux-immutable-state-invariant';
const rootReducer ={};

const middlewares = [ThunkMiddleware];
if(process.env.NODE_ENV!== 'production'){
//由于有函数式编程风格和 redux 机制上的限制，我们永远都不应该直接 “修改” state 的值。如果直接修改state值，会抛出异常提醒
    middlewares.push(immutablestate())
}
const enhancers = compose(
    applyMiddleware(...middlewares),
    (window && window.devToolsExtension?window.devToolsExtension():(f)=>f)
)
let createConfigStore ;
if(process.env.NODE_ENV === 'production'){
    createConfigStore = function(reducers, initialState){
        reducers = reducers || {};
        return createStore(combineReducers({...rootReducer,...reducers}), initialState);
    }
}else{
    createConfigStore = function(reducers, initialState = {}){
        reducers = reducers || {};
        return createStore(combineReducers({...rootReducer,...reducers}), initialState, enhancers);
    }
}
export default createConfigStore;